﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using CM.Properties;

namespace CM
{    
    public partial class Config : Window
    {
        #region DeclareConfig

        #region Random

        public string Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CM\configuration.xml";
        public string User = Environment.UserName;
        public string serverIP;

        #endregion

        #region location

        string location;

        #endregion

        #region Type

        string type;

        #endregion

        #region Language

        string language;

        #endregion

        #region online

        bool onlineMode;

        #endregion

        #region maxplayers

        string max_players;

        #endregion

        #region Ram

        string Ram;

        #endregion

        #region port

        string port;

        #endregion

        #region WorldName

        string Worldname;

        #endregion

        #endregion

        public Config()
        {
            #region GetConfig

            XmlDocument Doc = new XmlDocument();
            Doc.Load(Path);
            XmlElement Element = Doc.DocumentElement;

            location= Element.ChildNodes[0].InnerText;
            Ram = Element.ChildNodes[1].InnerText;
            serverIP = Element.ChildNodes[2].InnerText;
            port = Element.ChildNodes[3].InnerText;
            max_players = Element.ChildNodes[4].InnerText;

            if (Element.ChildNodes[5].InnerText == "True")
            {
                onlineMode = true;
            }
            else
            {
                onlineMode = false;
            }

            type = Element.ChildNodes[6].InnerText;
            language = Element.ChildNodes[7].InnerText;
            Worldname = Element.ChildNodes[8].InnerText;

            #region Setconfiginwindow

            InitializeComponent();

            LangBox.SelectedItem = language;
            TypeBox.SelectedItem = type;
            txtLocation.Text = location;
            textBlock1.Visibility = Visibility.Hidden;
            txtLocation.Visibility = Visibility.Visible;
            ServerIP.Text = serverIP;
            MemorySlider.Value = Double.Parse(Ram);
            Port.Text = port;
            MaxPlayers.Text = max_players;
            OnlineMode.IsChecked = onlineMode;
            WorldNameTb.Text = Worldname;

            #endregion

            #endregion
        }

        #region saveconfig

        int LastCheck()
        {
            if (MaxPlayers.Text.Length > 0 && Port.Text.Length > 0 && ServerIP.Text.Length > 0 && WorldNameTb.Text.Length > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            if (LastCheck() == 1)
            {
                XmlDocument Doc = new XmlDocument();
                Doc.Load(Path);
                XmlElement Element = Doc.DocumentElement;

                Element.ChildNodes[0].InnerText = txtLocation.Text;
                Element.ChildNodes[1].InnerText = MemorySlider.Value.ToString();
                Element.ChildNodes[2].InnerText = ServerIP.Text;
                Element.ChildNodes[3].InnerText = Port.Text;
                Element.ChildNodes[4].InnerText = MaxPlayers.Text;
                Element.ChildNodes[5].InnerText = OnlineMode.IsChecked.ToString();
                Element.ChildNodes[6].InnerText = TypeBox.Text;
                Element.ChildNodes[7].InnerText = LangBox.Text;
                Element.ChildNodes[8].InnerText = WorldNameTb.Text;

                Doc.Save(Path);
                Close();
            }
            else
            {
                lblLastCheck.Visibility = Visibility.Visible;
            }
        }

        #endregion

        #region setlocation

        private void DBBrowse_Click(object sender, RoutedEventArgs e)
        {
            #region folderDialog (dlg)

            var dlg = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dlg.ShowDialog();         

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string foldername = dlg.SelectedPath;
                txtLocation.Text = foldername;
                location = foldername;

                MessageBox.Show("Foldername: " + foldername);

                if (foldername.Length > 0)
                {
                    textBlock1.Visibility = Visibility.Hidden;
                    txtLocation.Visibility = Visibility.Visible;
                }
            }

            #endregion
        }

        #endregion

        #region check_for_right_inputs

        #region Ip_TextBox

        private void ServerIP_LostFocus(object sender, RoutedEventArgs e)
        {
            if (ServerIP.Text.Contains(".") || ServerIP.Text.ToLower() == "localhost")
            {
                IpErrorLbl.Visibility = Visibility.Hidden;
                serverIP = ServerIP.Text;
            }
            else
            {
                IpErrorLbl.Visibility = Visibility.Visible;
                IpErrorLbl.Content = "Please use a valid host";

                Dispatcher.BeginInvoke((ThreadStart)delegate
                {
                    ServerIP.Focus();
                    ServerIP.SelectAll();
                });
            }
        }

        private void ServerIP_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (ServerIP.Text.Length > 0)
            {
                IpErrorLbl.Visibility = Visibility.Hidden;
            }
        }

        #endregion

        #region Port

        private void Port_LostFocus(object sender, RoutedEventArgs e)
        {
            PortErrorLbl.Visibility = Visibility.Hidden;
        }

        private void Port_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (Port.Text.Length > 0)
            {
                try
                {
                    if (int.Parse(Port.Text) > 65535)
                    {
                        Port.Text = "65535";

                        PortErrorLbl.Content = "Highest port is 65535";
                        port = Port.Text;
                        PortErrorLbl.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        PortErrorLbl.Visibility = Visibility.Hidden;
                    }
                }
                catch
                {
                    Port.Clear();
                    PortErrorLbl.Content = "Numbers only please";
                    PortErrorLbl.Visibility = Visibility.Visible;
                }
            }
            else
            {
                PortErrorLbl.Visibility = Visibility.Hidden;
            }
        }

        #endregion

        #region Max_Players

        private void MaxPlayers_LostFocus(object sender, RoutedEventArgs e)
        {
            MaxPlayersErrorLbl.Visibility = Visibility.Hidden;
        }

        private void MaxPlayers_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (MaxPlayers.Text.Length > 0)
            {
                try
                {
                    int.Parse(MaxPlayers.Text);
                    max_players = MaxPlayers.Text;
                    MaxPlayersErrorLbl.Visibility = Visibility.Hidden;
                }
                catch
                {
                    MaxPlayers.Clear();

                    MaxPlayersErrorLbl.Visibility = Visibility.Visible;
                    MaxPlayersErrorLbl.Content = "Numbers only";
                }
            }
            else
            {
                MaxPlayersErrorLbl.Visibility = Visibility.Hidden;
            }
        }

        #endregion

        #region slider

        private void MemorySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                if (MemorySlider.Value > 0)
                {
                    MB.Content = MemorySlider.Value.ToString();
                }
            }
            catch
            {

            }
        }

        #endregion

        #region language

        private void LangBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            language = LangBox.Text;
        }

        #endregion

        #region type

        private void TypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            type = TypeBox.Text;
        }

        #endregion

        #region Worldname

        private void WorldNameTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            Worldname = WorldNameTb.Text;
        }

        #endregion

        #endregion
    }
}
